import{createRouter,createWebHistory} from "vue-router";
import Index from "../pages/Index.vue";

const routes = [
    {path: "/",component:Index},
    {path: "/login",name:'login',component: ()=>import("../pages/Login.vue")},
    {path:"/index",component:()=>import("../pages/Index.vue"),
        // 路由独享守卫
        // beforeEnter:(to,from) =>{
        //     console.log("路由独享守卫");
        //     console.log(to);
        //     console.log(from);
        //     return true; // 允许导航
        // }
    },
    {path: "/userList",component: ()=>import("../pages/UserList.vue"),
     children:[{
        path:"/userList/userUpdate/:id",name:'userUpdate',props:true,component:()=>import("../pages/UserUpdate.vue")
     }]
    },
    {path: "/userAdd",name:"userAdd",component: ()=>import("../pages/UserAdd.vue")}
]

const router = createRouter({
    history:createWebHistory(),
    routes
})
// //前置守卫
router.beforeEach((to,from,next)=>{
let token = sessionStorage.getItem("token");
    if (to.path === '/login') {
        next()
    }else {
        if (token != null) {
            next()
        }else {
            next({
                path:'/login'
            })
        }
    }
})
// //后置守卫
// router.afterEach((to,from)=>{
//     console.log("小老弟2")
//     console.log(to)
//     console.log(from)
// })
export default router